const mongoose=require("mongoose")
//暴露持久层函数
module.exports={
    getAll3,
    addOne3,
    deleteOne3,
    getOne3,
    upDateOne3,
    search3,
    searchClassify3,
    getAllGoods3
}
//获取商品数据
async function getAll3({currentPage,pageSize}){ 
   const infor= await mongoose.model("goodsModel").find().populate("classifyId").limit(pageSize-0).skip((currentPage-1)*pageSize)
   const sum= await mongoose.model("goodsModel").find().populate("classifyId").countDocuments()
   return {infor,sum}
}

//搜索数据
async function search3({currentPage,pageSize,condition,inputInfor}){ 
    const infor= await mongoose.model("goodsModel").find({[condition]:{$regex:inputInfor,$options:"$i"}}).populate("classifyId").limit(pageSize-0).skip((currentPage-1)*pageSize)
    const sum= await mongoose.model("goodsModel").find({[condition]:{$regex:inputInfor,$options:"$i"}}).populate("classifyId").countDocuments()
    return {infor,sum}
 }

//当搜索条件为分类时的搜索
async function searchClassify3({currentPage,pageSize,classify}){
    const infor=await mongoose.model("goodsModel").find({classifyId:classify}).populate("classifyId").limit(pageSize-0).skip((currentPage-1)*pageSize)
    const sum= await mongoose.model("goodsModel").find({classifyId:classify}).populate("classifyId").countDocuments()
    return {infor,sum}
}

//新增商品数据
async function addOne3(data){
    return await mongoose.model("goodsModel").create(data)
}

//删除数据
async function  deleteOne3(data){ 
    return await mongoose.model("goodsModel").deleteOne(data)
}
//获取一个数据
async function getOne3({_id}){
    return await mongoose.model("goodsModel").find({_id}).populate("classifyId")
 }
 //修改数据
 async function upDateOne3({data}){
     let _id=data.goods._id
    return await mongoose.model("goodsModel").updateOne({_id},data.goods)
}
//获取全部商品
async function getAllGoods3(){
    return await mongoose.model("goodsModel").find().populate("classifyId")
}